2016-07-15

Choices of packages

  • ggvis: both static and interactive graphics, interactive is very much a work in progress (Wickham)
  • plotly: has come a long way in the last 12 months, part of Carson Sievert's PhD thesis research. The beauty is that is builds directly onto ggplot2
  • animint: Hasn't progressed much in the last year, needs special copy of ggplot2 (Hocking, et al)
  • htmlwidgets: has progressed a lot in 12 months, providing the base for other packages, e.g. plotly (Chang)
  • rCharts, rbokeh, gridSVG, epivizr

ggvis

Built on the javascript library vega

library(ggvis)
data("economics", package = "ggplot2")

ggvis(economics, x=~date, y=~psavert)

economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points() %>%
  layer_smooths()

Interactivity

The coolest thing about ggvis is that plot parameters don't need to be static: they can be interactive

  • input_text(): text box
  • input_slider(): numeric slider
  • input_select(): dropdown box
  • input_checkbox(): checkbox

economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points(
    opacity := input_slider(label = "Opacity", 0,1)
  ) %>%
  layer_smooths(
    span = input_slider(label="Bandwidth", 0.05, 1),
    stroke := input_checkboxgroup(
      choices = c("Red" = "r", "Green" = "g", "Blue" = "b"),
      label = "Point color components",
      map = function(val) {
        rgb(0.8 * "r" %in% val, 0.8 * "g" %in% val, 0.8 * "b" %in% val)
      }
    )
  )

Attention grabber

  • ggvis grabs the R console
  • you cannot do any other R commands while an interactive ggvis plot is showing
  • exit by clicking on the stop icon on the plot panel

Your turn

The values provided for the slider (0.2, 1) look like they are not ideal. Change slider bounds so that they are more appropriate for this data.

Labels

all_values <- function(x) {
  if (is.null(x)) return(NULL)
  
  paste0(names(x), ": ", format(x), collapse = "<br />")
}

economics %>%
  ggvis(~unemploy, ~psavert) %>%
  layer_points() %>%
  add_tooltip(all_values, "hover") 
all_values_2 <- function(x) {
  if (is.null(x)) return(NULL)
  x <- economics[x$id, ]
  
  paste0(names(x), ": ", format(x), collapse = "<br />")
}

economics$id <- 1:nrow(economics)
economics %>%
  ggvis(~unemploy, ~psavert, key := ~id) %>%
  layer_points() %>%
  add_tooltip(all_values_2, "hover") 

Checkbox

model_type <- input_checkbox(label = "Use loess curve",
  map = function(val) if(val) "loess" else "lm")
economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points() %>%
    layer_model_predictions(model = model_type)

Your turn

  • Convert the model input to two radio buttons
  • Add a robust linear model choice, which means adding a third radio button
model_type <- input_radiobuttons(choices =c("linear"="lm","LOESS"= "loess","robust"="MASS::rlm"), selected = "lm", label="Model Type")
economics %>%
  ggvis(~date, ~psavert) %>%
  layer_points() %>%
    layer_model_predictions(model = model_type) %>%
    add_tooltip(all_values, "hover") 

plotly

The plotly package in R builds on the ggplot2 package, adding interactive elements to these plots. It translates plots to javascript.

library(plotly)
plot_ly(economics, x = date, y = unemploy / pop)

Or using ggplot2

ggplot(data=economics, aes(x = date, y = unemploy / pop)) +  
        geom_point() + geom_line()

ggplotly()

Still a work in progress

library(GGally)
p <- ggpairs(economics[,3:6])
ggplotly(p)

data(canada.cities, package = "maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
  borders(regions = "canada") +
  coord_equal() +
  geom_point(aes(text = name, size = pop), colour = "red", alpha = 1/2)
 ggplotly(viz)

Return to the RNA-Seq data

p <- ggplot(sig.tab, aes(x=C_S1_R2, y=C_S2_R1, label=genes)) +
  geom_point(alpha=0.1) 
ggplotly(p)

With a large data set it can be slow!

Your turn

Find which ggplot2 geoms are supported in plotly, and maybe one that is not?

Example, using Australian elections

library(eechidna)
launchApp(
  age = c("Age20_24", "Age85plus"),
  religion = c("Christianity", "Catholic", "NoReligion"),
  other = c("Unemployed", "Population", "MedianIncome")
)

Your turn

  • Go to the plotly github page examples and play with some of the examples.
  • Take one of the ggplot2 plots we have made so far in the workshop, and add interactive labelling to it.

Resources

Share and share alike

This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/ 3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.